package cn.walls1717.wallsmall.geteway.config;

import cn.walls1717.wallsmall.common.utils.R;
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
import com.alibaba.fastjson.JSON;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import static cn.walls1717.wallsmall.common.exception.BizCodeEnum.TOO_MANY_REQUEST_EXCEPTION;

/**
 * @author walls1717
 * @since 2022/11/29 14:12
 **/
@Configuration
public class SentinelConfig {
    public SentinelConfig() {
        GatewayCallbackManager.setBlockHandler((serverWebExchange, throwable) -> {
            R r = R.error(TOO_MANY_REQUEST_EXCEPTION.getCode(), TOO_MANY_REQUEST_EXCEPTION.getMsg());
            String error = JSON.toJSONString(r);
            return ServerResponse.ok().body(Mono.just(error), String.class);
        });
    }
}
